/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.aftersalesservice.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.DateUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.aftersalesservice.entity.PotentialAfterSalesService;
import cn.knet.sp.ncrm.modules.customer.aftersalesservice.service.PotentialAfterSalesServiceService;

/**
 * 成单信息Controller
 * @author 冯亚鹏
 * @version 2015-09-01
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/potentialAfterSalesService")
public class PotentialAfterSalesServiceController extends BaseController {

	@Autowired
	private PotentialAfterSalesServiceService potentialAfterSalesServiceService;
	
	@RequiresPermissions("customer:potentialAfterSalesService:view")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/customer/aftersalesservice/potentialAfterSalesServiceAjaxList";//ajax模式
	}

	@RequiresPermissions("customer:potentialAfterSalesService:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page<PotentialAfterSalesService> listAjaxData(PotentialAfterSalesService potentialAfterSalesService, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PotentialAfterSalesService> page = new Page<PotentialAfterSalesService>(request, response);
		page.setOrderBy("a.date_created DESC");
		if(potentialAfterSalesService!=null){
			if(potentialAfterSalesService.getEndDate()!=null){
				potentialAfterSalesService.setEndDate(DateUtils.addMilliseconds(potentialAfterSalesService.getEndDate(), DateUtils.TIME_23_59_50));
			}
		}
		page = potentialAfterSalesServiceService.findPage(page, potentialAfterSalesService);
		return page;
	}

	/**
	 * 成单展示index调整
	 * @author zhangbin
	 * @date 2015 /09/21 09:32:23
	 * @return the string
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "listOnShow")
	public String listOnShow() {
		return "modules/customer/aftersalesservice/potentialAfterSalesServiceOnShow";
	}


	/**
	 * 成单展示数据查询
	 * @author zhangbin
	 * @date 2015 /09/21 09:32:02
	 * @param potentialAfterSalesService the potential after sales service
	 * @param request the request
	 * @param response the response
	 * @param model the model
	 * @return the page
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "listOnShowDate")
	@ResponseBody
	public Page<PotentialAfterSalesService> listOnShowDate(PotentialAfterSalesService potentialAfterSalesService, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PotentialAfterSalesService> page = new Page<PotentialAfterSalesService>(request, response);
		if(potentialAfterSalesService!=null){
			if(potentialAfterSalesService.getEndDate()!=null){
				potentialAfterSalesService.setEndDate(DateUtils.addMilliseconds(potentialAfterSalesService.getEndDate(), DateUtils.TIME_23_59_50));
			}
			potentialAfterSalesService.setOnshow("1"); //固定为展示信息
			page = potentialAfterSalesServiceService.findPage(page, potentialAfterSalesService);
		}else{
			return null;
		}
		return page;
	}

	/**
	 * 成单批量修改展示状态
	 * @author zhangbin
	 * @date 2015 /09/21 09:31:42
	 * @param ids the ids
	 * @param onshow the onshow
	 * @return the object
	 */
	@RequiresPermissions("customer:potentialAfterSalesService:edit")
	@RequestMapping(value = "onShow")
	@ResponseBody
	public Object onShow(@RequestParam(value = "ids[]") String[] ids,String onshow) {
		AjaxMessage result = AjaxMessage.success();
		try {
			potentialAfterSalesServiceService.onShow(ids,onshow);
			if("0".equals(onshow)){
				result.setMessage("成单信息,关闭展示成功");
			}else {
				result.setMessage("成单信息,展示成功");
			}
		} catch (Exception e) {
			logger.warn("单信息修改失败", e);
			result.makeError("单信息修改失败!系统发生异常");
		}
		return result;
	}
	
	/**
	 *  TODO(查询成单信息列表，潜在客户)
	 * @author Gehaiyang
	 * @date 2015年9月9日 下午1:36:20
	 * @param potentialAfterSalesService
	 * @return
	 */
	@RequestMapping("listSalesData")
	@ResponseBody
	public Page<PotentialAfterSalesService> listSalesData(PotentialAfterSalesService potentialAfterSalesService) {
		Page<PotentialAfterSalesService> page = new Page();
		page.setList(potentialAfterSalesServiceService.findList(potentialAfterSalesService));
		return page;
	}
	
	@RequestMapping("save")
	@ResponseBody
	public AjaxMessage save(PotentialAfterSalesService potentialAfterSalesService){
		try {
			potentialAfterSalesServiceService.save(potentialAfterSalesService);
			AjaxMessage ajaxMessage = AjaxMessage.success("保存成单信息成功");
			return ajaxMessage;
		} catch (Exception e) {
			logger.error("保存成单信息失败",e);
			return AjaxMessage.error("保存成单信息失败");
		}
	}

	
}